package com.kordar.api.modular.unicomJS.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kordar.api.modular.unicomJS.ZOPPreOrderRequestImpl;
import com.kordar.api.modular.unicomJS.ZOPSubmitRequestImpl;
import com.kordar.api.modular.unicomJS.entity.Order;
import com.kordar.api.modular.unicomJS.entity.OrderTmp;
import com.kordar.api.modular.unicomJS.mapper.OrderMapper;
import com.kordar.api.modular.unicomJS.mapper.OrderTmpMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    final OrderMapper orderMapper;
    final OrderTmpMapper orderTmpMapper;

    @Override
    public boolean tmpOrder(ZOPPreOrderRequestImpl orderRequest, String orderNo) {
        OrderTmp order = new OrderTmp();
        order.setId(orderRequest.getNumber());
        order.setUserName(orderRequest.getCertName());
        order.setUserIdent(orderRequest.getCertNo());
        order.setUserPhone(orderRequest.getContactNum());
        order.setChannel(orderRequest.getChannel());
        order.setUserPostcode(orderRequest.getPostDistrictCode());
        order.setUserAddress(orderRequest.getPostProvince() +
                orderRequest.getPostCity() +
                orderRequest.getPostDistrict() +
                orderRequest.getPostAddr());
        order.setGoodId(orderRequest.getGoodsId());
        order.setOrderNo(orderNo);
        return orderTmpMapper.insert(order) > 0;
    }

    @Override
    public boolean updateOrder(ZOPSubmitRequestImpl submitRequest, OrderTmp orderTmp,
                               String uuid, String orderNo, String perNumber, String shortUrl) {

        Order order = new Order();
        order.setId(orderTmp.getId());
        order.setGoodId(orderTmp.getGoodId());
        order.setUserAddress(orderTmp.getUserAddress());
        order.setUserPostcode(orderTmp.getUserPostcode());
        order.setChannel(orderTmp.getChannel());
        order.setUserPhone(orderTmp.getUserPhone());
        order.setUserIdent(orderTmp.getUserIdent());
        order.setUserName(orderTmp.getUserName());
        order.setCity(submitRequest.getCityCode());
        order.setProvince(submitRequest.getProvinceCode());
        order.setUuid(uuid);
        order.setShortUrl(shortUrl);
        order.setPerNumber(perNumber);
        order.setOrderNo(orderNo);
        order.setPhone(submitRequest.getPhoneNum());

        return orderMapper.insert(order) > 0;
    }


    @Override
    public OrderTmp existTmpOrder(String orderId) {
        return orderTmpMapper.selectOne(new QueryWrapper<OrderTmp>().eq("id", orderId));
    }
}
